package cn.yi.medicalsys.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.yi.medicalsys.dao.OfficeDao;
import cn.yi.medicalsys.pojo.Office;
import cn.yi.medicalsys.utils.BaseDao;

public class OfficeDaoImpl extends BaseDao implements OfficeDao {

	@Override
	public Office findOfficeByNo(Integer officeNo) throws Exception {
		Office office = null;
		String preparedSql = "SELECT oid,officeName FROM office WHERE oid=? ";
		Object[] params = { officeNo };
		ResultSet rs = super.executeQuery(preparedSql, params);
		while (rs != null && rs.next()) {
			office = new Office();
			office.setoId(rs.getInt("oid"));
			office.setOfficeName(rs.getString("officeName"));
		}
		super.closeResource(rs);
		return office;
	}

	public static void main(String[] args) {
		OfficeDaoImpl sh = new OfficeDaoImpl();
		try {
			System.out.println(sh.findAllOffice().get(1).getOfficeName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<Office> findAllOffice() throws Exception {
		Office office = null;
		List<Office> list = new ArrayList<Office>();
		String preparedSql = "SELECT oid,officeName FROM office";
		ResultSet rs = super.executeQuery(preparedSql, null);
		while (rs != null && rs.next()) {
			office = new Office();
			office.setoId(rs.getInt("oid"));
			office.setOfficeName(rs.getString("officeName"));
			list.add(office);
		}
		super.closeResource(rs);
		return list;
	}

	@Override
	public int updateOfficeByName(Integer officeNo,String offficeName) throws Exception {
		StringBuffer preparedSql=new StringBuffer();
		List<Object> listParams=new ArrayList<Object>();
		preparedSql.append("UPDATE office SET oid=? ");
		listParams.add(officeNo);
		if(offficeName!=null && !offficeName.equals("")){
			preparedSql.append(",officeName=? ");
			listParams.add(offficeName);
		}
		preparedSql.append("WHERE oid=? ");
		listParams.add(officeNo);
		Object[] params=listParams.toArray();
		int row=super.executeUpdate(preparedSql.toString(), params);
		return row;
	}
}
